from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt, QRect
import sys
import ctypes


def get_screen_resolution_windows():
    user32 = ctypes.windll.user32
    screen_width = user32.GetSystemMetrics(0)  # 0 表示屏幕宽度
    screen_height = user32.GetSystemMetrics(1)  # 1 表示屏幕高度
    return screen_width, screen_height


class TransparentWindow(QWidget):
    def __init__(self):
        super().__init__()

        # 取屏幕分辨率 宽高
        self.screen_width, self.screen_height = get_screen_resolution_windows()

        # 设置窗口属性，使其点击穿透
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.WindowTransparentForInput)
        self.setAttribute(Qt.WA_TranslucentBackground, True)

        # 窗口尺寸
        self.setGeometry(0, 0, self.screen_width, self.screen_height)

    def paintEvent(self, event):
        painter = QPainter(self)

        # 画半透明红色矩形
        rect = QRect(50, 50, 100, 50)
        painter.fillRect(rect, QColor(255, 0, 0, 128))

        # 画边框
        pen = QPen(QColor(0, 255, 0, 128), 3, Qt.SolidLine)  # 边框颜色、宽度和线型
        painter.setPen(pen)
        painter.drawRect(self.rect())

    def mousePressEvent(self, event):
        # 重写鼠标点击事件，使其可以穿透
        event.ignore()

if __name__ == '__main__':
    app = QApplication(sys.argv)

    app.setApplicationName("MyApp")

    window = TransparentWindow()
    window.show()
    sys.exit(app.exec_())
